/*
 * Copyright (c) 2017. 深圳一七网络科技. All rights reserved.
 */

package com.fenxiangle.yueding.feature.login.presenter;

import android.text.TextUtils;

import com.fenxiangle.yueding.entity.bo.LoginPram;
import com.flyco.tablayout.widget.MsgView;
import com.suozhang.framework.entity.bo.UserInfoBo;
import com.suozhang.framework.framework.AM;
import com.suozhang.framework.utils.MD5Util;
import com.suozhang.framework.utils.T;
import com.suozhang.framework.utils.ValidatorUtil;
import com.suozhang.framework.utils.logger.Logger;
import com.fenxiangle.yueding.feature.login.contract.LoginContract;
import com.fenxiangle.yueding.feature.login.dependencies.login.DaggerLoginPresenterComponent;

import javax.inject.Inject;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * LIJUWEN
 *
 * @email yiyayiyayaoljw@gmail.com
 * @date 2017/4/11 9:31
 */
public class LoginPresenter implements LoginContract.Presenter {

    private LoginContract.View mView;


    @Inject

    LoginContract.Model mModel;


    public LoginPresenter(LoginContract.View mView) {
        this.mView = mView;
        DaggerLoginPresenterComponent.create().inject(this);
    }

    @Override
    public void start() {

    }


    @Override
    public void userThirdLogin(String openId,int type,Integer sex, String userName, String userHead,String registrationId) {
        if (TextUtils.isEmpty(openId)) {
            T.showShort("openId有误请重新授权！");
            return;
        }

        LoginPram loginPram = new LoginPram(openId,type,sex, userName, userHead,registrationId);
        mModel.userThirdLogin(loginPram).compose(mView.bindToLife()).subscribe(new Observer<UserInfoBo>() {
            @Override
            public void onSubscribe(Disposable d) {
                mView.showLoading(d);
            }

            @Override
            public void onNext(UserInfoBo s) {
                mView.dismissLoading();
                if (s.getIsPhone() == 0) {//未绑定手机号
                    mView.showBindPhone(s);
                } else {
                    mView.showLoginSuccess();
                    loginSuccess(s);
                    if (s.getIsNew()==1){///是否新用户
                        mView.showBindReferee(s);
                    }

                }

            }

            @Override
            public void onError(Throwable e) {
                mView.dismissLoading();
                mView.showError(e.getMessage());
            }

            @Override
            public void onComplete() {
                mView.dismissLoading();
            }
        });
    }

    @Override
    public void userLogin(String mobile, String code,String registrationId) {
        if (!ValidatorUtil.isMobile(mobile)) {
            T.showShort("手机号格式不正确！");
            return;
        }
        if (TextUtils.isEmpty(code)) {
            T.showShort("验证码不能为空！");
            return;
        }
        LoginPram loginPram = new LoginPram(mobile, code, registrationId);
        mModel.userLogin(loginPram).compose(mView.<UserInfoBo>bindToLife()).subscribe(new Observer<UserInfoBo>() {
            @Override
            public void onSubscribe(Disposable d) {
                mView.showLoading(d);
            }

            @Override
            public void onNext(UserInfoBo s) {

                loginSuccess(s);

                mView.dismissLoading();
                if (s.getIsNew() == 1) {
                    mView.showBindReferee(s);
                } else {
                    mView.showLoginSuccess();
                }

            }

            @Override
            public void onError(Throwable e) {
                mView.dismissLoading();
                mView.showError(e.getMessage());
            }

            @Override
            public void onComplete() {
                mView.dismissLoading();
            }
        });
    }

    @Override
    public void sendCode(String mobile) {
        if (!ValidatorUtil.isMobile(mobile)) {
            T.showShort("手机号格式不正确！");
            return;
        }
        LoginPram registerBo = new LoginPram(mobile);
        mModel.sendCode(registerBo).compose(mView.<LoginPram>bindToLife()).subscribe(new Observer<LoginPram>() {
            @Override
            public void onSubscribe(Disposable d) {
                mView.showLoading(d);
            }

            @Override
            public void onNext(LoginPram s) {
                mView.dismissLoading();
                mView.showCodeSuccess(s);
            }

            @Override
            public void onError(Throwable e) {
                mView.dismissLoading();
                mView.showError(e.getMessage());
            }

            @Override
            public void onComplete() {
                mView.dismissLoading();
            }
        });
    }

    @Override
    public void bindPhone(String userPhone, String code, String userUid) {
        if (!ValidatorUtil.isMobile(userPhone)) {
            T.showShort("手机号格式不正确！");
            return;
        }
        if (TextUtils.isEmpty(code)) {
            T.showShort("请输入验证码！");
            return;
        }
        LoginPram registerBo = new LoginPram();
        registerBo.setUserPhone(userPhone);
        registerBo.setCode(code);
        registerBo.setUserUid(userUid);
        mModel.bindPhone(registerBo).compose(mView.<UserInfoBo>bindToLife()).subscribe(new Observer<UserInfoBo>() {
            @Override
            public void onSubscribe(Disposable d) {
                mView.showLoading(d);
            }

            @Override
            public void onNext(UserInfoBo s) {
                mView.dismissLoading();
                if (s.getIsNew() == 1) {
                    mView.showBindReferee(s);
                } else {
                    mView.showLoginSuccess();
                }
                loginSuccess(s);
            }

            @Override
            public void onError(Throwable e) {
                mView.dismissLoading();
                mView.showError(e.getMessage());
            }

            @Override
            public void onComplete() {
                mView.dismissLoading();
            }
        });
    }



    private void loginSuccess(UserInfoBo value) {
        Logger.d("登录成功 ==== " + value.toString());
        AM.user().saveLoginResultCache(value == null ? null : value);
        mView.showLoginSuccess();
    }

}
